package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LibraryBean;
import com.example.myapplication.ShuJu.Re_Servlet;
import com.example.myapplication.library.LibraryDetailActivity;
import com.example.myapplication.library.LibraryMainActivity;

import java.util.List;

public class LibraryAdapter extends  RecyclerView.Adapter<LibraryAdapter.MyServer>{

    private Context context ;
    private List<LibraryBean.RowsBean> list;
    private View view;

    public LibraryAdapter(Context context, List<LibraryBean.RowsBean> list) {
        this.context = context;
        this.list = list;

    }

    @NonNull
    @Override
    public LibraryAdapter.MyServer onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_library,null);
        return new LibraryAdapter.MyServer(view);
    }



    @Override
    public void onBindViewHolder(@NonNull LibraryAdapter.MyServer holder, int position) {
           Glide.with(context).load(list.get(position).getImgUrl()).into(holder.library_img);
           holder.library_title.setText(list.get(position).getName());
           holder.library_locations.setText(list.get(position).getAddress());
           holder.library_time.setText(list.get(position).getBusinessHours());
           if(list.get(position).getBusinessState().equals("0")){
               holder.library_status.setText("未营业");
           }else if(list.get(position).getBusinessState().equals("1")){
               holder.library_status.setText("在营业");
           }

           holder.library_img.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent = new Intent(context, LibraryDetailActivity.class);
                   intent.putExtra("LibraryBean_ID",String.valueOf(list.get(position).getId()));
                   context.startActivity(intent);
               }
           });

    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class  MyServer extends RecyclerView.ViewHolder{

        private  TextView library_title;
        private  TextView library_locations;
        private  TextView library_time;
        private  TextView library_status;
        private  ImageView library_img;

        public MyServer(@NonNull View itemView) {
            super(itemView);
            library_img = itemView.findViewById(R.id.library_img);
            library_title = itemView.findViewById(R.id.library_title);
            library_locations = itemView.findViewById(R.id.library_locations);
            library_time = itemView.findViewById(R.id.library_time);
            library_status = itemView.findViewById(R.id.library_status);

        }
    }
}
